package com.alliky.home.mvp.presenter;

import com.alliky.core.entity.BannerEntity;
import com.alliky.core.entity.vo.BannerEntityInfo;
import com.alliky.core.entity.vo.CategoryVO;
import com.alliky.core.entity.vo.GoodsVO;
import com.alliky.core.base.BasePresenter;
import com.alliky.core.net.BaseObserver;
import com.alliky.core.utils.RxUtils;
import com.alliky.home.mvp.contract.HomeContract;
import com.alliky.home.mvp.model.HomeModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Description TODO
 * @Author wxianing
 * @Date 2020-12-18
 */
public class HomePresenter extends BasePresenter<HomeModel, HomeContract.View> implements HomeContract.Presenter {

    public HomePresenter(HomeContract.View view) {
        super(view);
    }

    @Override
    protected HomeModel createModel() {
        return new HomeModel();
    }

    @Override
    public void getBanner(HashMap param) {
        if (!isViewAttached()) return;
        mModel.getBanner(param)
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))// 请求与View周期同步
                .compose(RxUtils.schedulersTransformer())// 线程调度
                .compose(RxUtils.exceptionTransformer())// 网络错误的异常转换
                .subscribe(new BaseObserver<List<BannerEntity>>(mRootView) {
                    @Override
                    public void onResult(List<BannerEntity> bannerEntities) {
                        List<BannerEntityInfo> entityInfoList = new ArrayList<>();
                        for (BannerEntity entity : bannerEntities) {
                            BannerEntityInfo entityInfo = new BannerEntityInfo();
                            entityInfo.setBannerEntity(entity);
                            entityInfoList.add(entityInfo);
                        }
                        mRootView.showBannerResult(entityInfoList);
                    }
                });
    }

    @Override
    public void getCategory(HashMap param) {
        if (!isViewAttached()) return;
        mModel.getCategory(param)
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))// 请求与View周期同步
                .compose(RxUtils.schedulersTransformer())// 线程调度
                .compose(RxUtils.exceptionTransformer())// 网络错误的异常转换
                .subscribe(new BaseObserver<List<CategoryVO>>(mRootView) {
                    @Override
                    public void onResult(List<CategoryVO> list) {
                        mRootView.showCategory(list);
                    }
                });
    }

    @Override
    public void getHotGoods(HashMap param, boolean isRefresh) {
        if (!isViewAttached()) return;
        mModel.getHotGoods(param)
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))// 请求与View周期同步
                .compose(RxUtils.schedulersTransformer())// 线程调度
                .compose(RxUtils.exceptionTransformer())// 网络错误的异常转换
                .doOnSubscribe(this)
                .subscribe(new BaseObserver<List<GoodsVO>>(mRootView, false) {
                    @Override
                    public void onResult(List<GoodsVO> list) {
                        if (isRefresh) {
                            mRootView.finishRefresh();
                        } else {
                            mRootView.finishLoadMore();
                        }
                        mRootView.showHotGoods(list, isRefresh);
                        mRootView.setPageNum((int) param.get("pageNum") + 1);
                    }
                });
    }
}
